import { inject, observer } from 'mobx-react'
import { useEffect } from 'react'
import { history } from 'umi'
import store from './store'
import { isNullData } from '@/utils'

export default inject('userStore')(
    observer((props: any) => {
        // from: wechat 来源是微信
        const { id, from }: any = history.location.query || {}
        const { getBarriersInfo, resetBarriers } = store || {}
        const { userData } = props.userStore || {}
        const { mid_domain, platform_access_token } = userData || {}

        /** 跳转平台页面 */
        const gotoPlatform = () => {
            getBarriersInfo(data => {
                if (isNullData(data?.job_expect_list)) {
                    const callbackUrl = encodeURIComponent(window.location.origin + `/#/intended/result?id=${id}`)
                    window.location.href = `${mid_domain}/job-center/intended-position/index?token=${platform_access_token}&callback=${callbackUrl}&openKey=`
                } else {
                    history.replace({
                        pathname: '/intended/result',
                        query: { id, from },
                    })
                }
            })
        }
        useEffect(() => {
            gotoPlatform()
            return () => {
                resetBarriers()
            }
        }, [])

        return <>{props.children}</>
    }),
)
